package com.vgemv.jsutilitysdkdemo.module.StringValidator;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.Display.JsActivityContentDisplayManager.JsActivityContentDisplayManager;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdk.String.JsStringValidator;

import java.util.ArrayList;

public class StringValidatorActivity extends AppCompatActivity {


    private FrameLayout mfl;
    private EditText met;
    private RelativeLayout mrl_root;

    public Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);


        setupUi();

    }

    private void setupUi() {
        mrl_root = new RelativeLayout(this);
        mrl_root.setId(View.generateViewId());
        setContentView(mrl_root);

        {
            met = new EditText(this);
            met.setText("https://www.baidu.m3u8");
            met.setBackgroundColor(Color.argb(255,255,200,200));
            met.setId(View.generateViewId());
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,JsGeometryManager.pixelFromDp(60));
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            int margin = JsGeometryManager.pixelFromDp(5);
            lp.setMargins(margin,margin,margin,margin);
            mrl_root.addView(met,lp);

        }

        {
            mfl = new FrameLayout(this);
            mfl.setId(View.generateViewId());
            mfl.setBackgroundColor(Color.argb(255,255,200,200));
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lp.addRule(RelativeLayout.BELOW,met.getId());
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            mrl_root.addView(mfl,lp);
        }

        JsMenuFragment menuFragment = new JsMenuFragment();
        menuFragment.moveToParentView(this,mfl);
        final int itemSize = JsGeometryManager.pixelFromDp(80);
        menuFragment.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView view, int index) {
                int spanCount = view.mMenuViewVm.mi_spanCount;
                int hPadding = (JsGeometryManager.getScreenSizeInPixel().getWidth() - spanCount * itemSize)/(spanCount + 2);
                int vPadding = JsGeometryManager.pixelFromDp(20);

                Rect rect = new Rect();
                if(index == 0) {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                else if(index == view.vms().size() - 1) {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                else {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                return rect;
            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView f) {
                JsMenuViewVm vm = new JsMenuViewVm();
                vm.mi_spanCount = 4;
                return vm;
            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView f, Context context) {
                return null;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView fragment) {

            }

            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                ArrayList<JsMenuItemVm> models = new ArrayList<JsMenuItemVm>();

                JsMenuItemStyleDefault style = new JsMenuItemStyleDefault();
                style.mI_backgroundColor = Color.BLACK;
                style.mI_titleTextColor = Color.WHITE;
                style.mi_height = itemSize;
                style.mi_width = itemSize;

                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(0,"是否是RtmpUrl","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(1,"是否是HlsUrl","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(2,"Info","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(3,"Warn","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(4,"Error","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(5,"Success","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(6,"ForceUndisplayReminder","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
                return models;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView fragmentAlpha, int i, JsMenuItemVm model) {
                switch (i) {
                    case 0:{
                       //1是否是rtmp
                        if(JsStringValidator.isValidRtmpAddress(met.getText().toString())) {
                            JsLogManager.logc_d("Validator","This is a valid rtmp url");
                            JsActivityContentDisplayManager.sharedInstance().displayMessage("ValidRtmpUrl",StringValidatorActivity.this);
                        }
                        else {
                            JsLogManager.logc_d("Validator","This is not a valid rtmp url");
                            JsActivityContentDisplayManager.sharedInstance().displayMessage("InvalidRtmpUrl",StringValidatorActivity.this);
                        }
                        break;
                    }
                    case 1:{
                        String urlString = met.getText().toString();
                        if(JsStringValidator.isValidHlsAddress(urlString)) {
                            JsLogManager.logc_d("Validator","This is a valid hls url");
                            JsActivityContentDisplayManager.sharedInstance().displayMessage("ValidHlsUrl",StringValidatorActivity.this);
                        }
                        else {
                            JsLogManager.logc_d("Validator","This is not a valid hls url");
                            JsActivityContentDisplayManager.sharedInstance().displayMessage("InvalidHlsUrl",StringValidatorActivity.this);
                        }
                       //是否是hls
                        break;
                    }
                    case 2: {

                        break;
                    }
                    case 3: {

                        break;
                    }
                    case 4: {

                        break;
                    }
                    case 5 : {

                        break;
                    }
                    case 6: {

                        break;
                    }
                    default:{
                        break;
                    }
                }
            }
        };
    }
}